package jjoms.controller;

import java.util.List;

import javax.annotation.Resource;

import jjoms.model.main.ContentsVO;
import jjoms.model.main.MainService;
import jjoms.userException.UpdatedHitsInfoException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/main")
public class MainController {

	@Resource(name = "mainServiceImpl")
	private MainService mainService;
	
	@RequestMapping("/NewsearchAll.moja")
	public ModelAndView NewsearchAll(String keyword) {
		ModelAndView mv = null;
		
		ContentsVO cvo = null;
		try{
			System.out.println("searchAll : " + keyword);
			mainService.NewsearchAllContents(keyword);
		}catch(UpdatedHitsInfoException e){
			cvo = e.getCvo();
			System.out.println("catch cvo"+ cvo);
		}finally{
			System.out.println("cvo: " + cvo);
			if (cvo== null) {
				mv = new ModelAndView("insertConfirm.info");
				
			} else {
				mv = new ModelAndView("allsearch_view.main", "cvo", cvo);
			}
		}
		
		
		return mv;
	}
	
	@RequestMapping("/autoKeyword.moja")
	@ResponseBody
	public List<String> autoKeyword(String keyword, String kind){
		System.out.println("keyword : " + keyword + " kind : " + kind);
		System.out.println(mainService.autoKeyword(keyword, kind));
		return mainService.autoKeyword(keyword, kind);
	}
}
